<template>
    <div>
         <!-- 面包屑导航区域 -->
		<el-breadcrumb separator-class="el-icon-arrow-right">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>商品管理</el-breadcrumb-item>
			<el-breadcrumb-item>参数列表</el-breadcrumb-item>
		</el-breadcrumb>

        <!-- 卡片视图区域 -->
		<el-card>
            <!-- 警告区域 -->
            <el-alert title="注意：只允许为第三级分类设置相关参数" type="warning" show-icon="" :closable="false"></el-alert>

            <el-row class="cat_opt">
                <el-col>
                    <span>选择商品分类：</span>
                    <el-cascader expand-trigger="hover" :options="catelist" 
                    :props="cateProps" v-model="selectedCataKeys" @change="handleChange">

                    </el-cascader>
                </el-col>
            </el-row>

            <!-- tab 页签区域 -->
            <el-tabs v-model="activeName" @tab-click="handleTabClick">
                <el-tab-pane label="动态参数" name="many">
                    <el-button type="primary" size="mini" :disabled="isBtnDisabled" @click="addDialogVisible = true">添加参数</el-button>
                    <el-table :data="manyTableData" border stripe>
                        <el-table-column type="expand">
                            <template slot-scope="scope">
                                <el-tag v-for="(item, i) in scope.row.attr_vals" :key="i" closable>{{item}}</el-tag>
                            </template>
                        </el-table-column>
                        <el-table-column type="index"></el-table-column>
                        <el-table-column label="参数名称" prop="attr_name"></el-table-column>
                        <el-table-column label="操作">
                            <template>
                                <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
                                <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-tab-pane>
                <el-tab-pane label="静态属性" name="only">
                    <el-button type="primary" size="mini" :disabled="isBtnDisabled" @click="addDialogVisible = true">添加属性</el-button>
                    <el-table :data="onlyTableData" border stripe>
                        <el-table-column type="expand"></el-table-column>
                        <el-table-column type="index"></el-table-column>
                        <el-table-column label="参数名称" prop="attr_name"></el-table-column>
                        <el-table-column label="操作">
                            <template>
                                <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
                                <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-tab-pane>
            </el-tabs>

        </el-card>

        <el-dialog
          title="添加动态参数/静态属性"
        :visible.sync="addDialogVisible"
        width="30%" @close="addDialogClosed">
            <!-- 主体区域 -->
            <el-form :model="addForm" status-icon :rules="addFormRules" ref="addFormRef"
            label-width="100px">
                <el-form-item :label="titleText" prop="attr_name">
                    <el-input type="text" v-model="addForm.attr_name" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <!-- 底部区域 -->
            <span slot="footer" class="dialog-footer">
                <el-button @click="addDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addParams()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data(){
        return {
            queryInfo:{
                type:[],
                pagenum:0,
                pagesize:1
            },
            // 商品分类的数据列表
            catelist:[],
            cateProps:{
                value:'cat_id',
                label:'cat_name',
                children:'children'
            },
            //级联选择框
            selectedCataKeys:[],
            //被激活的页签的名称
            activeName:'many',
            // 动态参数的数据
            manyTableData:[],
            // 静态参数的数据
            onlyTableData:[],
            // 控制添加对话框的显示与隐藏
            addDialogVisible: false,
            addForm:{
                attr_name:''
            },
            addFormRules:{
                attr_name:[{required:true,message:"",trigger:'blur'}]
            }
        }
    },
    created(){
        this.getCateList()
    },
    methods:{
        async getCateList(){
            const {data:res} = await this.$http.get("/vip/acategories/get",{params:this.queryInfo})
            console.log(res)
            if(res.meta.status !== 200)
            {
                return this.$message.error('获取商品分类失败')
            }
            this.catelist = res.data.cates
        },
        // 级联选择框选中项变化，会触发这个函数
        handleChange(){
            this.getParamsData()
        },
        // tab 页签点击事件的处理函数
        handleTabClick(){
            this.getParamsData()
        },
        async getParamsData(){
             if(this.selectedCataKeys.length !== 3){
                this.selectedCataKeys = []
                return 
            }

            const {data:res} = await this.$http.get(`/vip/categories/${this.cateId}/attributes`,{params:{sel:this.activeName}})
            
            if(res.meta.status !== 200)
            {
                return this.$message.error('获取商品分类失败')
            }

            res.data.forEach(item => {
                item.attr_vals = item.attr_vals ? item.attr_vals.split(' ') : []
            });

            if(this.activeName ===  "many"){
                this.manyTableData = res.data
            }else{
                this.onlyTableData = res.data
            }
        },
        addDialogClosed(){
            this.$refs.addFormRef.resetFields()
        },
        addParams(){
            this.$refs.addFormRef.validate(valid => {
                if(!valid) reutrn
                this.$http.post('')
            })
        }
    },
    computed:{
        isBtnDisabled(){
            if(this.selectedCataKeys.length !== 3){
                return true;
            }
            return false;
        },
        cateId(){
            if(this.selectedCataKeys.length === 3){
                return this.selectedCataKeys[2];
            }
            return null
        },
        titleText(){
            if(this.activeName === "many"){
                return "动态参数"
            }
            return "静态属性"
        }
    }
}
</script>

<style lang="less" scoped>
.cat_opt{
    margin: 15px 0;
}
</style>